//
//  DeviceInfo.m
//  QuPaiYunDemo
//
//  Created by 占益民 on 2018/3/28.
//  Copyright © 2018年 占益民. All rights reserved.
//

#import "DeviceInfo.h"
#include <sys/param.h>
#include <sys/mount.h>


@implementation DeviceInfo

+(NSString *)getDeviceFreeDiskSpaceInByte{
    struct statfs buf;
    unsigned long long freeSpace = -1;
    if (statfs("/var", &buf) >= 0) {
        freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bfree);
    }
    NSString *str = [NSString stringWithFormat:@"手机剩余存储空间为：%0.2lld MB",(freeSpace/1024/1024)];
    /// 总大小
    float totalsize = 0.0;
    /// 剩余大小
    float freesize = 0.0;
    /// 是否登录
    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
    if (dictionary)
    {
        NSNumber *_free = [dictionary objectForKey:NSFileSystemFreeSize];
        freesize = [_free unsignedLongLongValue]*1.0/(1024);
        
        NSNumber *_total = [dictionary objectForKey:NSFileSystemSize];
        totalsize = [_total unsignedLongLongValue]*1.0/(1024);
    } else
    {
        NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
    }
    NSLog(@"totalsize = %.2f, freesize = %f",totalsize/1024/1024/1024, freesize/1024);
    return str;
}

@end
